﻿//----------------------------------------------------------------------------------------------------
// The Visual Game handler holds the active game contents
//----------------------------------------------------------------------------------------------------

package HG.Visual
{
	import flash.display.*;
	import flash.events.*;
	
	import HG.GameService;
	
	public class VisualEncryptionIcon extends MovieClip
	{
		var m_currentValue:Number;
		
		//----------------------------------------------------------------------------------------------------
		public function VisualEncryptionIcon() 
		{
			m_currentValue = 0;
		}
		
		//----------------------------------------------------------------------------------------------------
		public function Clear()
		{
			var index:Number = 0;
			var listLength:Number = 8;
			
			for (index = 0; index < listLength; ++index)
			{
				this["Item_" + index].visible = false;
			}
		}
		
		//----------------------------------------------------------------------------------------------------
		public function GetValue():Number
		{
			return m_currentValue;
		}
		
		//----------------------------------------------------------------------------------------------------
		public function SetValue(num:Number)
		{
			Clear();
			
			if (num <= 0 || num >= 16)
			{
				m_currentValue = 0;
				this.visible = false;
				return;
			}
			this.visible = true;
				
			m_currentValue = num;
			
			var binary:Array = dec2bin(num);
			
			var index:Number = 0;
			var listLength:Number = binary.length;
			
			for (index = 0; index < listLength; ++index)
			{
				if (binary[index] == 1)
				{
					this["Item_" + index].visible = true;
					this["Item_" + (4 + index)].visible = true;
				}
			}
		}
		
		//----------------------------------------------------------------------------------------------------
		function dec2bin(num:Number):Array
		{
			var arrayLength = 4;
			
			var binaryString:String = new String();
			var binaryArray:Array = new Array();
			
			while(num > 0)
			{
				num /= 2;
				if(Math.floor(num) == num)
				{
					binaryArray.push(0);
				}else{
					binaryArray.push(1);
				}
				num = Math.floor(num);
			}
			
			while (binaryArray.length < arrayLength)
			{
				binaryArray.push(0);
			}
			
			return binaryArray;
		}
		
		//----------------------------------------------------------------------------------------------------
	}
}